package com.check.controller;

import java.io.PrintWriter;
import java.net.URLDecoder;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.check.pojo.MarkRole;
import com.check.pojo.PageObject;
import com.check.service.MarkRoleService;

@Controller
public class MarkRoleController extends BaseController{
	@Autowired
	@Qualifier("markRoleService")
	private MarkRoleService markRoleService;
	
	
	@RequestMapping("markRoleQuery.do")
	public ModelAndView findMarkRole(HttpServletRequest request,HttpServletResponse response,
			@ModelAttribute("pageObject")PageObject pageObject){
		if(pageObject.getCurrent()==0){
			pageObject.setCurrent(1);
		}
		List<MarkRole> markRoleList = markRoleService.findAllMarkRole(pageObject);
		pageObject.setTotal(markRoleService.findMarkRoleCount());
		pageObject.setUrl("markRoleQuery.do");
		saveSysLog(RE_MAROLE, LOG_QUERY, request);
		return new ModelAndView("page/markrole/markRoleList")
					.addObject("markRoleList",markRoleList)
					.addObject("path", request.getContextPath())
					.addObject("page",pageObject.toPage());
	}
	
	@RequestMapping("toAddMarkRole.do")
	public ModelAndView toAddMarkRole(HttpServletRequest request,HttpServletResponse response){
		String treeJson=markRoleService.getTreeJson();
		return new ModelAndView("page/markrole/markRoleAdd").addObject("path",request.getContextPath())
				    .addObject("treeJson",treeJson);
	}
	
	@RequestMapping(value="addMarkRole.do",method = RequestMethod.POST)
	public void addMarkRole(HttpServletRequest request,HttpServletResponse response,
			@ModelAttribute("markRole")MarkRole markRole){
			PrintWriter out = null;
			String flag = "0";
			
			try {
				out = response.getWriter();
				markRole.setM_name(URLDecoder.decode(request.getParameter("m_name"),  "UTF-8"));
				String[] m_w_ids = request.getParameter("w_m_ids").split(",");
				markRole.setM_w_ids(m_w_ids);
				if(markRoleService.insertMarkRole(markRole)){
					flag = "1";
				}
				saveSysLog(RE_MAROLE, LOG_ADD, request);
			} catch (Exception e) {
				e.printStackTrace();
			}finally{
				out.print(flag);
				if(out != null){
					out.flush();
					out.close();
				}
			}
		}
	
	@RequestMapping("findMarkRole.do")
	public void findMarkRole(HttpServletRequest request,HttpServletResponse response){
		PrintWriter out=null;
		String m_id=request.getParameter("m_id");
		String flag = "0";
		try{
			response.setContentType("text/html;charset=gbk");
			out=response.getWriter();
			MarkRole findMarkRole = markRoleService.findByMarkRoleID(m_id);
			if(findMarkRole!=null){
				flag="1";
			}else{
				flag="0";
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			out.print(flag);
			if(out != null){
				out.flush();
				out.close();
			}
		}
		
	}
	
	@RequestMapping("toUpdMarkRole.do")
	public ModelAndView toUpdMarkRole(HttpServletRequest request,HttpServletResponse response) {
		String m_id=request.getParameter("m_id");
		MarkRole markRole = markRoleService.findByMarkRoleID(m_id);
		String treeJson=markRoleService.getCheckedTreeJson(m_id);
		return new ModelAndView("page/markrole/markRoleUpdate")
		           .addObject("path", request.getContextPath())
		           .addObject("markRole", markRole)
		           .addObject("treeJson",treeJson);
	}
	
	@RequestMapping("updateMarkRole.do")
	public void updateMarkRole(HttpServletRequest request,HttpServletResponse response,@ModelAttribute("markRole") MarkRole markRole) {
		PrintWriter out = null;
		String flag = "0";
		
		try {
			out = response.getWriter();
			markRole.setM_name(URLDecoder.decode(request.getParameter("m_name"),"UTF-8"));
			String[] m_w_ids = request.getParameter("w_m_ids").split(",");
			markRole.setM_w_ids(m_w_ids);
			if(markRoleService.updateMarkRole(markRole)){
				flag = "1";
			}
			saveSysLog(RE_MAROLE, LOG_UPDATE, request);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			out.print(flag);
			if(out != null){
				out.flush();
				out.close();
			}
		}
	}
	
	
	@RequestMapping("deleteMarkRole.do")
	public void deleteMarkRole(HttpServletRequest request,HttpServletResponse response) {
		String m_id=request.getParameter("m_id");
		PrintWriter out = null;
		String flag = "0";
		
		try {
			out = response.getWriter();
			if(markRoleService.deleteMarkRole(m_id)){
				flag = "1";
			}
			saveSysLog(RE_MAROLE, LOG_DELETE, request);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			out.print(flag);
			if(out != null){
				out.flush();
				out.close();
			}
		}
	}
}
